package com.ostrich.a2104gao5

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/16 10:15
 * @Description : Test11
 */

fun main(){

    /******数组*******/
    val ints = intArrayOf(0,3,7)
    val arrays = arrayOf("a",2,"45")
    arrays.forEach {
        println(it)
    }
    arrays.forEachIndexed{index,item ->
        println("下标为 $index 值为 $item")
    }
    for (a in arrays) {
        println(a)
    }
    println(arrays[2])
    /******List*******/
    //List:单列集合，可重复
    val list = listOf<String>("Jason","Jack","Rose")//不可变,只读
//    println(list[4])//不安全调用
    println(list.getOrElse(4){"unknown"})//安全调用
    println(list.getOrNull(4))//安全调用
    println(list.getOrNull(4) ?: "unknown")//安全调用

    val mutableList = mutableListOf<String>("Jason","Jack","Rose")//可变
    mutableList.add("Jimmy")
    mutableList += "Jimmy2"
    mutableList.remove("Jimmy2")
    mutableList -= "Jimmy2"
    //将rose去掉，java遍历判断再删除
    mutableList.removeIf {
        it == "Rose"
    }
    println(mutableList)

    //list转成mutableList：不可变-》可变
    val toMutableList = list.toMutableList()
    //mutableList转成list：可变-》不可变
    val toList = mutableList.toList()

    //遍历
    mutableList.forEach {
        println(it)
    }
    mutableList.forEachIndexed{index,item ->
        println("下标为 $index 值为 $item")
    }
    for (a in mutableList) {
        println(a)
    }
    //解构
    val (n1,n2,n3) = mutableList
    println(n1)
    println(n2)
    println(n3)
    /******Set*******/
    //Set：单列集合，不可重复
    val set = setOf<String>("Jason","Jack","Rose") //不可变
//    set[0]
//    println(set.elementAt(3)) //不安全
    println(set.elementAtOrElse(3){"unkonwm"})
    println(set.elementAtOrNull(3) ?: "unkonwm")

    val mutableSet = mutableSetOf<String>("Jason","Jack","Rose")  //可变
    mutableSet += "Jimmy"
    mutableSet -= "Rose"
    println(mutableSet)
    /******Map:双列集合*******/
    val map = mapOf<String,Int>("zhangsan" to 20,"lisi" to 40,"wangwu" to 30)
    println(map)


    val map2 = mapOf<String,Int>(Pair("zhangsan",20), Pair("lisi",40), Pair("wangwu",30))
    println(map2)

    println(map["zhangsan66"])
    println(map.get("zhangsan"))
    println(map.getOrElse("zhangsan66"){"默认值"})
    println(map.getOrDefault("zhangsan55","默认值"))

    map.forEach {
        println("键:${it.key} 值:${it.value}")
    }
    map.forEach { (a: String, b: Int) ->
        println("键:$a 值:$b")

    }

    val  mutableMap = mutableMapOf<String,Int>("zhangsan" to 20,"lisi" to 40,"wangwu" to 30)
    mutableMap += "zhaoliu" to 40
    mutableMap.put("haha",34)
    mutableMap["zhangsan"] = 50
    println(mutableMap)


    println(mutableMap.getOrPut("zhangsan"){56})
    println(mutableMap)
}
